/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the koala4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @Author: GuoYang
 */

package com.koalacloud.koala.zhaikucloud.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koalacloud.koala.common.core.util.R;
import com.koalacloud.koala.common.log.annotation.SysLog;
import com.koalacloud.koala.zhaikucloud.entity.MallPanelContent;
import com.koalacloud.koala.zhaikucloud.service.MallPanelContentService;
import com.koalacloud.koala.zhaikucloud.util.EsDataOperation;
import com.koalacloud.koala.zhaikucloud.util.EsQueryOperation;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 版块内容
 *
 * @Author: GuoYang
 * @date 2022-02-07 15:36:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/mallPanelContent")
@Tag(description = "mallPanelContent" , name = "版块内容管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MallPanelContentController {

    private final MallPanelContentService mallPanelContentService;


    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param mallPanelContent 版块内容
     * @return
     */
    @Operation(description = "分页查询", summary = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('mallPanelContent_view')")
    public R getMallPanelContentPage(Page page, MallPanelContent mallPanelContent) {
        return R.ok(mallPanelContentService.page(page, Wrappers.query(mallPanelContent)));
    }


    /**
     * 通过id查询版块内容
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mallPanelContent_view')")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(mallPanelContentService.getById(id));
    }

    /**
     * 新增版块内容
     *
     * @param mallPanelContent 版块内容
     * @return R
     */
    @Operation(summary = "新增版块内容", description = "新增版块内容")
    @SysLog("新增版块内容")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('mallPanelContent_add')")
    public R save(@RequestBody MallPanelContent mallPanelContent) {
        return R.ok(mallPanelContentService.save(mallPanelContent));
    }

    /**
     * 修改版块内容
     *
     * @param mallPanelContent 版块内容
     * @return R
     */
    @Operation(summary = "修改版块内容", description = "修改版块内容")
    @SysLog("修改版块内容")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('mallPanelContent_edit')")
    public R updateById(@RequestBody MallPanelContent mallPanelContent) {
        return R.ok(mallPanelContentService.updateById(mallPanelContent));
    }

    /**
     * 通过id删除版块内容
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除版块内容", description = "通过id删除版块内容")
    @SysLog("通过id删除版块内容")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('mallPanelContent_del')")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(mallPanelContentService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param mallPanelContent 查询条件
     * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('mallPanelContent_export')" )
    public List<MallPanelContent> export(MallPanelContent mallPanelContent, Integer[] ids) {
        return mallPanelContentService.list(Wrappers.lambdaQuery(mallPanelContent).in(ArrayUtil.isNotEmpty(ids), MallPanelContent::getId, ids));
    }


}
